<template>
	<view class="container">
		<!-- <view class="navbar acea-row row-around">
			<view class="item acea-row row-center-wrapper" :class="{ on: navOn === 1 }" @click="onNav(1)">{{$t(`未使用`)}}</view>
			<view class="item acea-row row-center-wrapper" :class="{ on: navOn === 2 }" @click="onNav(2)">{{$t(`已使用/过期`)}}</view>
		</view> -->
    
    <view class="new-coupon-list"  v-if="couponsList.length">
        <view class="item"  v-for="(item,index) in couponsList" :key="index">
            <view class="hd">
              <view class="price"><text class="unit">￥</text>{{item.coupon_price}}</view>
              <view class="desc">
                <view class="text1">
                  {{item.coupon_title}}
                </view>
                <view class="text2">有效期至：{{item.end_time}}</view>
              </view>
            </view>
            <view class="bd">
              <view class="rule-box">
                <view class="rule" @tap="handleRule(item, index)">使用规则<image :src="item.showRule ? '../static/arrow_up.png' : '../static/arrow_down.png'" class="arrow-img" /></view>
                <view class="btn"  @click="useCoupon(item)">{{$t(`去使用`)}}</view>
              </view>
              <view class="rule-text" v-if="item.showRule">
                1、自绑定日起180天内使用有效，租期包含任意法定节假日(含公休及调休)及前一天不能使用;
                2、单笔订单租期满2天及以上可用，适用于经济或舒适车型，仅限非首次取车订单可用，仅减免最低一日【车辆租赁费及门店服务费】，最高可减200元，其他费用正常收取;
                3、不可与一嗨其他优惠共享，不可重复使用，仅限一嗨国内自驾业务使用;
                4、该券仅限账户本人使用，不可转让，实际承租人非本人订单不可用。
              </view>
            </view>
        </view>
    </view>
    
	<!-- 	<view class='coupon-list' v-if="couponsList.length">
			<view class='item acea-row row-center-wrapper' v-for='(item,index) in couponsList' :key="index" :class="{svip: item.receive_type === 4}" @click="useCoupon(item)">
				<view class="theme-color-bg acea-row row-center-wrapper" style="height: 100%;">
					<view class='money' :class='item._type == 0 ? "moneyGray" : ""'>
						<view>{{$t(`￥`)}}<text class='num'>{{item.coupon_price}}</text></view>
						<view class="pic-num" v-if="item.use_min_price > 0">{{$t(`满`)}}{{item.use_min_price}}{{$t(`元可用`)}}</view>
						<view class="pic-num" v-else>{{$t(`无门槛券`)}}</view>
					</view>
				</view>
				<view class='text'>
					<view class='condition'>
						<view class="name line2">
							<view class="line-title" :class="item._type === 0 ? 'bg-color-huic' : 'bg-color-check'" v-if="item.applicable_type === 0">{{$t(`通用劵`)}}</view>
							<view class="line-title" :class="item._type === 0 ? 'bg-color-huic' : 'bg-color-check'" v-else-if="item.applicable_type === 1">{{$t(`品类券`)}}</view>
							<view class="line-title" :class="item._type === 0 ? 'bg-color-huic' : 'bg-color-check'" v-else>{{$t(`商品券`)}}</view>
							<image src="https://taxi.raxan.xyz/wechat/static/images/fvip.png" class="pic" v-if="item.receive_type===4"></image>{{$t(item.coupon_title)}}
						</view>
					</view>
					<view class='data acea-row row-between-wrapper'>
						<view>{{item.add_time}}-{{item.end_time}}</view>
						<view class='bnt gray' v-if="item._type==0">{{$t(item._msg)}}</view>
						<view class='bnt theme-color-bg' v-else>{{$t(item._msg)}}</view>
					</view>
				</view>
			</view>
		</view> -->
    
		<view class='noCommodity' v-if="!couponsList.length && page === 2">
			<view class='pictrue'>
				<!-- <image :src="imgHost + '/statics/images/noCoupon.png'"></image> -->
        <image src="../static/no_coupon.png"></image>
        <view class="no-data-txt">暂无优惠券可使用哦~</view>
			</view>
		</view>
    
    <view class="operate-box" @tap="jumpCouponRecord">
        <view class="btn">
          优惠券历史记录
        </view>
    </view>
    
		<!-- #ifdef MP -->
		<!-- <authorize @onLoadFun="onLoadFun" :isAuto="isAuto" :isShowAuth="isShowAuth" @authColse="authColse"></authorize> -->
		<!-- #endif -->
		<!-- #ifndef MP -->
		<!-- <home></home> -->
		<!-- #endif -->
	</view>
</template>

<script>
	import {
		getUserCoupons
	} from '@/api/api.js';
	import {
		toLogin
	} from '@/libs/login.js';
	import {
		mapGetters
	} from "vuex";
	// #ifdef MP
	import authorize from '@/components/Authorize';
	// #endif
	import home from '@/components/home';
	import colors from '@/mixins/color.js';
	import {
		HTTP_REQUEST_URL
	} from '@/config/app';
	export default {
		components: {
			// #ifdef MP
			authorize,
			// #endif
			home
		},
		mixins: [colors],
		data() {
			return {
				imgHost: HTTP_REQUEST_URL,
				couponsList: [],
				loading: false,
				isAuto: false, //没有授权的不会自动授权
				isShowAuth: false, //是否隐藏授权
				navOn: '',
				page: 1,
				limit: 15,
				finished: false,
			};
		},
		computed: mapGetters(['isLogin']),
		watch: {
			isLogin: {
				handler: function(newV, oldV) {
					if (newV) {
						this.getUseCoupons();
					}
				},
				deep: true
			}
		},
		onLoad() {
			if (this.isLogin) {
				this.getUseCoupons();
			} else {
				toLogin();
			}
		},
		onReachBottom() {
			this.getUseCoupons();
		},
		methods: {
			onNav: function(type) {
				this.navOn = type;
				this.couponsList = [];
				this.page = 1;
				this.finished = false;
				this.getUseCoupons();
			},
      jumpCouponRecord(){
        uni.navigateTo({
          url: `/pages/users/user_coupon/record`
        })
      },
      handleRule(item, index){
          let {couponsList} = this
        	this.couponsList = couponsList.map((item, idx) => {
            if(idx == index){
              item.showRule = !item.showRule
            }
            return item;
          })
      },
			useCoupon(item) {
				if (this.navOn == 2) return
				let url = '';
				if (item.category_id == 0 && item.product_id == '') {
					// url = '/pages/goods/goods_list/index?title=默认'
					url = '/pages/car_index/index';
				}
				if (item.category_id != 0) {
					// url = `/pages/goods/goods_list/index?title=${item.coupon_title}&coupon_category_id=${item.category_id}`
					url = '/pages/car_index/index';
				}
				if (item.product_id != '') {
					let arr = item.product_id.split(',');
					let num = arr.length;
					if (num == 1) {
						// url = '/pages/goods_details/index?id=' + item.product_id
						url = '/pages/car_index/index';
					} else {
						// url = '/pages/goods/goods_list/index?productId=' + item.product_id + '&title=默认'
						url = '/pages/car_index/index';
					}
				}
				uni.switchTab({
					url: url
				});
			},
			/**
			 * 授权回调
			 */
			onLoadFun: function() {
				this.getUseCoupons();
			},
			// 授权关闭
			authColse: function(e) {
				this.isShowAuth = e
			},
			/**
			 * 获取领取优惠券列表
			 */
			getUseCoupons: function() {
				let that = this;
				if (that.loading || that.finished) {
					return;
				}
				that.loading = true;
				uni.showLoading({
					title: that.$t(`正在加载…`)
				});
				getUserCoupons({
					page: this.page,
					limit: this.limit,
          types: this.navOn
				}).then(res => {
					that.loading = false;
					uni.hideLoading();
					that.couponsList = that.couponsList.concat(res.data);
					that.finished = res.data.length < that.limit;
					that.page += 1;
				}).catch(err => {
					that.loading = false;
					uni.showToast({
						title: err,
						icon: 'none'
					});
				});
			}
		}
	}
</script>

<style scoped lang="scss">
  .container{
    padding-bottom: 180rpx;
  }
	.money {
		display: flex;
		flex-direction: column;
		justify-content: center;
	}

	.pic-num {
		color: #ffffff;
		font-size: 24rpx;
	}
	.coupon-list .item .text{
		padding: 14rpx 10rpx;
	}
	.coupon-list .item .text .condition {
		display: flex;
	}

	.coupon-list .item .text .condition .name {
		font-size: 24rpx;
		font-weight: 500;
		line-height: 34rpx;
		/* display: flex;
		align-items: center; */
	}

	.coupon-list .item .text .condition .pic {
		width: 30rpx;
		height: 30rpx;
		display: block;
		margin-right: 10rpx;
		display: inline-block;
		vertical-align: middle;
	}

  .new-coupon-list{
    padding: 24rpx 32rpx;
    .item{
      margin-bottom: 24rpx;
      padding: 32rpx 30rpx 0 30rpx;
      background: #FFF;
      border-radius: 16rpx;
      .hd{
        display: flex;
        align-items: center;
        border-bottom: 1rpx solid #E7E7E7;
        padding-bottom: 23rpx;
        .price{
          margin-right: 40rpx;
          font-weight: 500;
          font-size: 50rpx;
          color: #FF5700;
          .unit{
            font-size: 30rpx;
            color: #FF5700;
          }
        }
        .desc{
          .text1{
            margin-bottom: 8rpx;
            font-weight: 600;
            font-size: 32rpx;
            color: #000000;
          }
          .text2{
            display: flex;
            font-weight: 400;
            font-size: 24rpx;
            color: #777B82;
          }
        }
      }
      .bd{
        .rule-box{
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 24rpx 0;
            .rule{
              display: flex;
              align-items: center;
              font-size: 24rpx;
              color: rgba(0,0,0,0.6);
              image{
                width: 40rpx;
                height: 40rpx;
                margin-left: 5rpx;
              }
            }
            .btn{
              padding: 12rpx 30rpx;
              background: #FF7200;
              border-radius: 200rpx 200rpx 200rpx 200rpx;
              font-size: 26rpx;
              color: #FFFFFF;
              &.gray{
                 background: #ccc;
              }
            }
        }
        .rule-text{
          padding-bottom: 20rpx;
          font-weight: 400;
          font-size: 24rpx;
          color: rgba(0,0,0,0.6);
          line-height: 40rpx;
        }
      }
    }
  }
  
  .operate-box{
    width: 100%;
    position: fixed;
    left: 0;
    bottom: 0;
    background: #FFF;
    padding: 20rpx 0;
    .btn{
     display: block;
     margin: 0 auto;
     width: 400rpx;
     height: 96rpx;
     line-height: 96rpx;
     text-align: center;
     border-radius: 12rpx 12rpx 12rpx 12rpx;
     border: 2rpx solid #C5C5C5;
     font-size: 32rpx;
     color: rgba(0,0,0,0.9);
    }
  }
  
	.condition .line-title {
		/* width: 70rpx; */
		height: 36rpx !important;
		padding: 0 5px;
		line-height: 32rpx;
		text-align: center;
		box-sizing: border-box;
		background: rgba(255, 247, 247, 1);
		border: 1px solid var(--theme-color-left);
		opacity: 1;
		border-radius: 20rpx;
		font-size: 18rpx !important;
		color: var(--theme-color-left);
		margin-right: 12rpx;
		text-align: center;
		display: inline-block;
		vertical-align: middle;
	}

	.condition .line-title.bg-color-huic {
		border-color: #BBB !important;
		color: #bbb !important;
		background-color: #F5F5F5 !important;
	}
</style>

<style lang="scss" scoped>
	.navbar {
		position: fixed;
		top: 0;
		left: 0;
		width: 100%;
		height: 90rpx;
		background-color: #FFFFFF;
		z-index: 9;

		.item {
			border-top: 5rpx solid transparent;
			border-bottom: 5rpx solid transparent;
			font-size: 30rpx;
			color: #999999;

			&.on {
				border-bottom-color: var(--theme-color-left);
				color: #282828;
			}
		}
	}

	.coupon-list {
		margin-top: 122rpx;
	}

	.noCommodity {
		margin-top: 200rpx;
	  .no-data-txt{
	    margin-top: 10rpx;
	    font-size: 26rpx;
	    color: #B9B9BD;
      text-align: center;
	  }
	}
</style>
